package MapReduce.Demo14_GenderCount;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import utils.JobSubmit;

import java.io.IOException;

/**
 * @Author lixinlei
 * @Date 2023/3/23 11:06
 */
public class GenderCountApp {

    public static class GCMapper extends Mapper<LongWritable, Text,Text, NullWritable>{
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, NullWritable>.Context context) throws IOException, InterruptedException {

            String gender = value.toString();

            Counter counter_boy = context.getCounter("MapperGender","boy");
            Counter counter_girl = context.getCounter("MapperGender","girl");

            if(gender.equals("男")){
                counter_boy.increment(1);
                counter_girl.increment(10);
            }

            context.write(value,NullWritable.get());
        }
    }

    public static class GCReducer extends Reducer<Text,NullWritable,Text,NullWritable>{
        @Override
        protected void reduce(Text key, Iterable<NullWritable> values, Reducer<Text, NullWritable, Text, NullWritable>.Context context) throws IOException, InterruptedException {

            Counter groupNums = context.getCounter("groups", "num");

            groupNums.increment(1);

            for (NullWritable value : values) {
                context.write(key,value);
            }
        }
    }

    public static void main(String[] args) {

        JobSubmit.submitBaseJob(
                GenderCountApp.class,
                args
        );

    }

}
